package com.qishenyuan.competition.vo;

import com.qishenyuan.competition.model.Teamup;
import com.qishenyuan.competition.utils.AliOssUtil;
import com.qishenyuan.competition.utils.DateUtil;
import lombok.Data;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author Jacky
 * @since 2025-03-17
 */
@Data
public class TeamUpDetailResp {

    private Integer id;

    private String publisher;

    private String avatar;

    private String content;

    private String createTime;

    private List<String> pictures;

    private String title;

    private String contract;

    private Integer tag;

    private Integer type;


    public static TeamUpDetailResp buildVo(Teamup teamup){
        TeamUpDetailResp resp = new TeamUpDetailResp();
        resp.setId(teamup.getId());
        resp.setContent(teamup.getContent());
        resp.setCreateTime(DateUtil.format(teamup.getCreateTime(),DateUtil.FORMAT_WITH_SPLIT));
        List<String> imageUrls = Optional.ofNullable(teamup.getPictures())
                .map(TeamUpDetailResp::string2List)
                .orElseGet(Collections::emptyList)
                .stream()
                .map(AliOssUtil::generatePreSignedUrl)
                .collect(Collectors.toList());
        resp.setPictures(imageUrls);
        resp.setTitle(teamup.getTitle());
        resp.setContract(teamup.getContract());
        resp.setTag(teamup.getTag());
        resp.setType(teamup.getType());
        return resp;
    }

    // 把String转换成list，用“;”分隔开
    private static List<String> string2List(String str) {
        List<String> result = Arrays.asList(str.split(";"));
        result = result.stream().filter(s -> !s.isEmpty()).collect(Collectors.toList());
        return result;
    }
}
